package com.myosw.quartz;

import java.util.Date;

import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.PropertiesConfiguration;
import org.apache.commons.lang.time.DateFormatUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.SchedulerFactory;
import org.quartz.SimpleTrigger;
import org.quartz.Trigger;
import org.quartz.impl.StdSchedulerFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import static org.quartz.TriggerBuilder.newTrigger;
import static org.quartz.SimpleScheduleBuilder.simpleSchedule;
import static org.quartz.CronScheduleBuilder.dailyAtHourAndMinute;
import static org.quartz.JobBuilder.newJob;
import org.quartz.CalendarIntervalScheduleBuilder;
public class JobManager
{
	private static Logger _log = LoggerFactory.getLogger(JobManager.class);
	private static PropertiesConfiguration quartzProperties;
	private static Scheduler sched ;
	
	
	
	public void run() throws SchedulerException  
	{
		
		try {
			quartzProperties = new PropertiesConfiguration("quartz.properties");
		} catch (ConfigurationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		//创建一个简单的应用并且运行这个应用
		/**
		 * 一旦通过StdSchedulerFactory.getDefaultScheduler()获得一个scheduler ，那么应用将不会结束，
		 * 除非调用scheduler.shutdown()方法。
		 */
		sched = new StdSchedulerFactory().getScheduler();
		   _log.info("*********************启动应用*********************");
	        
		   _filterJob();//关键字过滤
		   _searchJob();//搜索统计
		   _visitJob();//访问统计
      _nutchJob();//爬虫
       
       sched.start();
       
        /**
         * 关闭应用
         */
      //  sched.shutdown();
	}
	
	
	public static void shutdown() throws SchedulerException 
	{
		if(sched!=null) 
		{
			sched.shutdown();
			 _log.info("*********************关闭应用*********************");
		}
	}
	
	
	/**
	 * 关键字过滤
	 * @throws SchedulerException
	 */
	private void _filterJob() throws SchedulerException 
	{
		
		JobDetail filterJob = newJob(FilterJob.class)//要被执行的Java类。
                .withIdentity("filterJob")//名称
                .build();
        _log.info("*********************创建filterJob*********************");
    Date triggerStartTime = DateUtils.addMinutes(new Date(), 1);//一分钟后执行
		Trigger filterTrigger = newTrigger()
        		.withIdentity("filterTrigger")
        		.withSchedule(simpleSchedule().
        				withIntervalInHours(quartzProperties.getInt("filterTrigger"))//设置循环时间
        				.repeatForever()//指定触发器将无限期地重复。
        				)//设置循环时间
        		
        		.startAt(triggerStartTime)
        		.build();
        
        sched.scheduleJob(filterJob, filterTrigger);
       _log.info(filterJob.getKey() + " " +
       					"作业 将开始运行 : " + triggerStartTime+" " +
       				"循环运行间隔："+((SimpleTrigger) filterTrigger).getRepeatInterval() / 1000 +"s   " +
       						"执行次数"+((SimpleTrigger) filterTrigger).getRepeatCount() ); 
		
	}
	
	
	/**
	 * 搜索图统计
	 * @throws SchedulerException
	 */
	private void _searchJob() throws SchedulerException
	{
		JobDetail searchlogJob = newJob(SearchlogJob.class)//要被执行的Java类。
	               .withIdentity("searchlogJob")//名称
	               .build();
	       _log.info("*********************创建mysearchlogJob*********************");
	   Date triggerStartTime = DateUtils.addMinutes(new Date(), 2);//二分钟后执行
			Trigger searchlogTrigger = newTrigger()
	       		.withIdentity("searchlogTrigger")
	       		.withSchedule(simpleSchedule().
	       				withIntervalInMinutes(quartzProperties.getInt("searchlogTrigger"))//设置循环时间
	       				.repeatForever()//指定触发器将无限期地重复。
	       				)//设置循环时间
	       		
	       		.startAt(triggerStartTime)
	       		.build();
	       
	       sched.scheduleJob(searchlogJob, searchlogTrigger);
	      _log.info(searchlogTrigger.getKey() + " " +
	      					"作业 将开始运行 : " + triggerStartTime+" " +
	      					"循环运行间隔："+((SimpleTrigger)  searchlogTrigger).getRepeatInterval() / 1000 +"s  " +
	      					" 执行次数"+((SimpleTrigger) searchlogTrigger).getRepeatCount() ); 
	      
	}
	
	/**
	 * 访问统计
	 * @throws SchedulerException
	 */
	private void _visitJob() throws SchedulerException
	{
		JobDetail visitlogJob = newJob(VisitlogJob.class)//要被执行的Java类。
	              .withIdentity("visitlogJob")//名称
	              .build();
	      _log.info("*********************创建visitlogJob*********************");
	  Date triggerStartTime = DateUtils.addMinutes(new Date(),3);//三分钟后执行
			Trigger visitlogTrigger = newTrigger()
	      		.withIdentity("visitlogTrigger")
	      		.withSchedule(simpleSchedule().
	      				withIntervalInMinutes(quartzProperties.getInt("visitlogTrigger"))//设置循环时间
	      				.repeatForever()//指定触发器将无限期地重复。
	      				)//设置循环时间
	      		
	      		.startAt(triggerStartTime)
	      		.build();
	      
	      sched.scheduleJob(visitlogJob, visitlogTrigger);
	     _log.info(visitlogJob.getKey() + " " +
	    		 		"作业 将开始运行 : " + triggerStartTime+" " +
	     				"循环运行间隔："+((SimpleTrigger)  visitlogTrigger).getRepeatInterval() / 1000 +"s  " +
	     					" 执行次数"+((SimpleTrigger) visitlogTrigger).getRepeatCount() ); 
	    
		
	}
	
	/**
	 * 爬虫
	 * @throws SchedulerException
	 */
	private void _nutchJob() throws SchedulerException
	{
		JobDetail nutchJob = newJob(NutchJob.class)//要被执行的Java类。
	             .withIdentity("nutchJob")//名称
	             .build();
	     _log.info("*********************创建nutchJob*********************");
	  Date startTime = DateUtils.addMinutes(new Date(), 10);//钟后执行
			Trigger nutchTrigger = newTrigger()
	     		.withIdentity("nutchTrigger")
	     		.withSchedule(simpleSchedule().
	      				withIntervalInMinutes(quartzProperties.getInt("nutchTrigger"))//设置循环时间
	      				.repeatForever()//指定触发器将无限期地重复。
	      				)//设置循环时间
	     		.startAt(startTime)
	     		.build();
	     
	     sched.scheduleJob(nutchJob, nutchTrigger);
	    _log.info(nutchJob.getKey() + 
	    		" 作业 将开始运行 : " + startTime+
	    		" 循环运行间隔："+((SimpleTrigger) nutchTrigger).getRepeatInterval() / 1000 +"s  " +
	    		" 执行次数"+((SimpleTrigger) nutchTrigger).getRepeatCount() ); 
	    
	     
	}
	
	public static void main(String[] args) throws SchedulerException {
		JobManager test = new JobManager();
		test.run();
	}
}
